;layui.define(function(e) {
    layui.use(['table','layer','form','laydate'], function(){
        var $ = layui.$
            ,layer = layui.layer
            ,form = layui.form
            ,table = layui.table
            ,laydate = layui.laydate;

        var currPage = 1;
        //展示已知数据
        table.render({
            elem: '#idTest'
            , id: 'listReload'//生成 Layui table 的标识 id，必须提供，用于后文刷新操作
            , url: hostUrl + '/order/queryOrderList'
            ,cols: [[ //标题栏
            	{field: 'order_id', title: '订单号'}
                ,{field: 'brand_name', title: '品牌'}
                ,{field: 'series_name', title: '车系'}
                ,{field: 'car_type_name', title: '车型', minWidth: 120}
                ,{field: 'customer_name', title: '客户姓名'}
                ,{field: 'sex', title: '性别'}
                ,{field: 'phone', title: '手机号'}
                ,{field: 'level', title: '客户级别'}
                ,{field: 'create_time', title: '订单日期',minWidth: 160}
                ,{field: 'create_time', title: '交车日期',templet: function(d){
                        if(d.orderStatu == '已交车'){
                            return d.create_time;
                        }else{
                            return '-';
                        }
                 }}
                ,{field: 'orderStatu', title: '交车状态'}
                ,{field: 'store_sale_acc_name', title: '销售顾问'}
                ,{field: 'tool',title: '操作',fixed: 'right', minWidth:188, align:'center',templet: function(d){
                    // 0-已定未交 1-已交车 2-待审批 3-审核通过 4-审核驳回
                    var tools = $("#barDemo a");
                    var html = "";
                    for(var i=0;i<tools.length;i++) {
                        // 按钮名称
                        var value = tools[i].getAttribute("value");
                        if(d.orderStatu == '已交车' && value == '销售上报') {
                            html += tools[i].outerHTML;
                        } else {
                            if(value != '销售上报') {
                                html += tools[i].outerHTML;
                            }
                        }
                    }
                    return html;
                 }}
            ]]
            ,height: 500
            ,cellMinWidth: 120
            ,page: true //是否显示分页
            , done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                //得到当前页码
                currPage = curr;
                $('.layui-table-fixed-r').removeClass('layui-hide');
            }
        });
        //表刷新方法
        var reloadTable = function () {

            table.reload("listReload", { //此处是上文提到的 初始化标识id
                where: {
                    brandOrSeriesOrTypeName: $("#brandOrSeriesOrTypeName").val(),
                    startDate: $("#startDate").val(),
                    endDate: $("#endDate").val()
                }, page: {
                    curr: currPage //重新从第 page 页开始 默认从第一页开始重载
                }
            });
        };

        // 查询按钮绑定点击事件
        $("#searchBtn").on("click", function () {
            reloadTable();
        })

        //日期控件
        layui.use(['laydate'], function(){
            var laydate = layui.laydate;
            //常规用法
            laydate.render({
                elem: '#startDate'
            });

            laydate.render({
                elem: '#endDate'
            });
        });

        
    //监听工具条
    table.on('tool(listTable)', function(obj){
        var data = obj.data;
        if(obj.event === 'details'){
            Layer.openDialogWithCallBack("零售訂單詳情", hostUrl+'/retailorder/retailorderDetail.do?oper=detail&orderId=' + data.order_id,"500px","800px");
        } else if(obj.event === 'soldOutShlef'){
         	var clueId=data.customer_id;
        	var orderId=data.order_id;
        	var carId=data.car_id;
         	 layer.confirm('确定上报吗?', function(index){
              	  $.axse(
            		        hostUrl + "/order/orderReport",
            		        {"clueId":clueId,"orderId":orderId,"carId":carId},
            		        function (obj) {
            		        	 
            		        	if(obj.data=='1'){
                                  
            		        		Layer.showSucMsg("上报成功");
            		        		location.reload();
            		        		
         						}else{
         							
         							Layer.showSucMsg(obj.data);
         						}
            		        },
            		        function () {
            		            layer.msg("请求失败");
            		        }
            		    ) 
            		    layer.close(index);
             });
        }else if(obj.event === 'edit'){
            Layer.openDialogWithCallBack("零售訂單编辑", hostUrl+'/retailorder/retailorderAdd.do?oper=edit&orderId=' + data.order_id,"500px","800px",function () {
                // 编辑结束 刷新列表
                reloadTable();
            });
        }
    });

        var $ = layui.$, active = {
            getCheckData: function(){ //获取选中数据
                var checkStatus = table.checkStatus('idTest')
                    ,data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
            ,getCheckLength: function(){ //获取选中数目
                var checkStatus = table.checkStatus('idTest')
                    ,data = checkStatus.data;
                layer.msg('选中了：'+ data.length + ' 个');
            }
            ,isAll: function(){ //验证是否全选
                var checkStatus = table.checkStatus('idTest');
                layer.msg(checkStatus.isAll ? '全选': '未全选')
            }
        };

        $('.layadmin-takerates .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
         
        //执行一个laydate实例
        laydate.render({
            elem: '#date'
        });

        form.render();

        //订单池：按钮“新建订单”
        $("#retailorderAdd").click(function () {
            Layer.openDialogWithCallBack("修改资源", hostUrl+'/retailorder/retailorderAdd.do?oper=add',"500px","800px",function () {
                location.reload();
            });
        });

         //车辆信息：点击按钮“新建客户”
         $("#newCustomers").click(function () {
            var index = layer.open({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: '新建客户'
                ,content: 'newCustomers.html'
                ,area: ['500px', '500px']
                ,skin:'my_skin'
                ,maxmin: true
            });
            layer.full(index);
        });

        $(".check_more").click(function () {
            var index = layer.tab({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: '更多参数配置'
                ,content: 'more.html'
                ,area: ['500px', '700px']
                ,maxmin: true
//                 ,success: function(layero, index){
//                     弹框加载成功页面参数传递给弹框
//                     var body = layer.getChildFrame('body', index);
//                     var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
// //                        console.log(body.html()) //得到iframe页的body内容
//                     var paramId=body.find("input[name='id']");
//                     paramId.val(111);
//                 }
            });
            layer.full(index);
        })
        
    });

    
    
    //输出模块
    e("list", {})
});